package com.jiyi.activity.leader;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jiyi.bean.maintenance.WorkStatisticsBean;
import com.shizhefei.view.indicator.FragmentListPageAdapter;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.shizhefei.view.indicator.IndicatorViewPager.IndicatorFragmentPagerAdapter;
import com.shizhefei.view.indicator.ScrollIndicatorView;
import com.shizhefei.view.indicator.slidebar.ColorBar;
import com.shizhefei.view.indicator.transition.OnTransitionTextListener;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.utils.GlideUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PersonWorkStatisticsActivity extends ActivityBase{

	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;

	@BindView(R.id.id_stickynavlayout_indicator)
	ScrollIndicatorView mIndicator;
	@BindView(R.id.id_stickynavlayout_viewpager)
	ViewPager mViewPager;

	@BindView(R.id.ivLogo)
	ImageView ivLogo;
	@BindView(R.id.tvUserName)
	TextView tvUserName;

	IndicatorViewPager indicatorViewPager;
	private LayoutInflater inflate;

	private String[] names = { "维保任务", "维修任务" };

	//用户对象
	WorkStatisticsBean mWorkStatisticsBean;
	//月份
	String mouth;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_person_work_statistics);
		ButterKnife.bind(this);
		init();
	}
	

	private void init() {
		inflate = LayoutInflater.from(this);
		getIntentData();
		initIndicator();
	}

	

	private void getIntentData() {
		mouth = getIntent().getStringExtra("mouth");
		mWorkStatisticsBean = new Gson().fromJson(getIntent().getStringExtra("work_user"), WorkStatisticsBean.class);
		if (null == mWorkStatisticsBean) {
			ToastUtil.getInstances().showMsg("没有此用户");
			finish();
			return;
		}
		
		tvUserName.setText(mWorkStatisticsBean.getUser_name());
		GlideUtil.load(this, mWorkStatisticsBean.getHead_path(), ivLogo);
	}


	private void initIndicator() {
		inflate = LayoutInflater.from(this);
		mIndicator.setScrollBar(new ColorBar(this, Color.parseColor("#16B0F4"),
				5));

		// 设置滚动监听
		int selectColorId = R.color.tab_top_on;
		int unSelectColorId = R.color.tab_top;
		int selectSizeId = R.dimen.tab_text_select;
		int unSelectSizeId = R.dimen.tab_text_noselect;

		mIndicator.setOnTransitionListener(new OnTransitionTextListener()
				.setValueFromRes(this, selectColorId, unSelectColorId,
						selectSizeId, unSelectSizeId));

		indicatorViewPager = new IndicatorViewPager(mIndicator, mViewPager);
		indicatorViewPager
				.setAdapter(new MyAdapter(getSupportFragmentManager()));
		indicatorViewPager.setPageOffscreenLimit(1);
	}
	
	private class MyAdapter extends IndicatorFragmentPagerAdapter {

		public MyAdapter(FragmentManager fragmentManager) {
			super(fragmentManager);
		}

		@Override
		public int getCount() {
			return names.length;
		}

		@Override
		public View getViewForTab(int position, View convertView,
				ViewGroup container) {
			if (convertView == null) {
				convertView = inflate.inflate(R.layout.tab_top, container,
						false);
			}
			TextView textView = (TextView) convertView;
			textView.setText(names[position % names.length]);
			textView.setPadding(20, 0, 20, 0);
			
			return convertView;
		}

		@Override
		public Fragment getFragmentForPage(int position) {
			Fragment curFragment = null;
			Bundle bundle = null;
			switch (position) {
			case 0:
				// 维保任务
				curFragment = new WbWorkStatisticsFragment();
				bundle = new Bundle();
				bundle.putString("mouth", mouth);
				bundle.putString("id", mWorkStatisticsBean.getUser_id());
				curFragment.setArguments(bundle);
				break;
			case 1:
				// 维修任务
				curFragment = new WxWorkStatisticsFragment();
				bundle = new Bundle();
				bundle.putString("mouth", mouth);
				bundle.putString("id", mWorkStatisticsBean.getUser_id());
				curFragment.setArguments(bundle);
				break;
			default:
				break;
			}

			return curFragment;
		}

		@Override
		public int getItemPosition(Object object) {
			return FragmentListPageAdapter.POSITION_NONE;
		}

	};

	@OnClick({ R.id.tvBack })
	public void onClick(View view) {
		Intent intent = null;
		switch (view.getId()) {
		case R.id.tvBack:
			finish();
			break;
		default:
			break;
		}
	}
}
